package com.wn.main.ui.other

import android.content.Context
import android.content.Intent
import android.view.KeyEvent
import com.blankj.utilcode.util.GsonUtils
import com.google.android.material.tabs.TabLayoutMediator
import com.wn.common.constant.KEY_DATA
import com.wn.common.model.SystemList
import com.wn.common.model.SystemSecondList
import com.wn.common.provider.SearchServiceProvider
import com.wn.framework.base.BaseActivity
import com.wn.framework.ext.onClick
import com.wn.main.R
import com.wn.main.databinding.ActivityArticleSystemDetailBinding
import com.wn.main.ui.system.adapter.SystemViewPageAdapter
import dagger.hilt.android.AndroidEntryPoint

/**
 * @Author: wn
 * @Date: 2025/5/26 17:06
 * @Description: 体系 列表 详情页 tab + viewpager2
 */
@AndroidEntryPoint
class ArticleSystemDetailActivity : BaseActivity<ActivityArticleSystemDetailBinding>() {

  override fun getLayoutRes() = R.layout.activity_article_system_detail

  private var tabLayoutMediator: TabLayoutMediator? = null

  // tabs 与 ViewPager2 内容
  private var datas: MutableList<SystemSecondList>? = null

  override fun initView() {
    mBinding.apply {
      initTabAndFragment()
      /**
       * 点击去搜索页
       */
      ivSearch.onClick {
        SearchServiceProvider.toSearch(this@ArticleSystemDetailActivity)
      }
    }
  }

  override fun initData() {
    val dataJson = intent.getStringExtra(KEY_DATA)
    val bean = GsonUtils.fromJson(dataJson, SystemList::class.java)
    // 设置 标题
    mBinding.title = bean.name
    // 获取 二级列表
    datas = bean.children
  }

  /**
   * 初始化 TabLayout 和 ViewPager2
   */
  private fun initTabAndFragment() {
    mBinding.apply {
      /**
       * 初始化 TabLayout 和 ViewPager2
       */
      viewPager.apply {
        // 可左右滑动
        isUserInputEnabled = true
        // 禁用预加载
        offscreenPageLimit = 1
        /**
         * 这里第一种写法
         */
/*
        adapter = object : FragmentStateAdapter(this@ArticleSystemDetailActivity) {

          override fun getItemCount() = datas?.size ?: 0

          override fun createFragment(position: Int): Fragment =
            ArticleListFragment.newInstance(datas?.get(position)?.id ?: 0)
        }
*/

        /**
         * 这里试试第二种写法
         */
        datas?.let {
          adapter = SystemViewPageAdapter(supportFragmentManager, lifecycle, it)
        }
      }
      /**
       * tabLayout和viewPager2关联起来
       */
      tabLayoutMediator = TabLayoutMediator(tabHome, viewPager) { tab, position ->
        // 设置 Tab 的标题
        tab.text = datas?.get(position)?.name ?: ""
      }.apply {
        attach()
      }
    }
  }

  override fun onDestroy() {
    super.onDestroy()
    tabLayoutMediator?.detach()
  }

  override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
      // 结束当前页面
      finish()
      return true
    }
    return super.onKeyDown(keyCode, event)
  }

  companion object {
    /**
     * 启动 体系 列表 详情页
     * @param context Context
     * @param jsonData String
     */
    fun start(context: Context, jsonData: String) {
      Intent(context, ArticleSystemDetailActivity::class.java).apply {
        putExtra(KEY_DATA, jsonData)
        context.startActivity(this)
      }
    }
  }
}